const domain=require('domain');
const events=require('events');

let emitter1=new events.EventEmitter();
let domain1=domain.create();
domain1.on('error',(err)=>console.log(`error ${err.message} caught by domain1.`));
domain1.add(emitter1);
emitter1.on('error',(err)=>console.log(`error ${err.message} caught by emitter1.`));
emitter1.emit('error',new Error('Handled by emitter.'));
emitter1.removeAllListeners('error');
emitter1.emit('error',new Error('Handled by domain1.'));

let domain2=domain.create();
domain2.on('error',(err)=>console.log(`error ${err.message} caught by domain2`));
domain2.run(()=>{
    let emitter2=new events.EventEmitter();
    emitter2.emit('error',new Error('Handled by domian2.'));
});

domain1.remove(emitter1);
emitter1.emit('error',new Error('Uncaught Exception.'));
